<%-- 
    Document   : action
    Created on : Dec 13, 2012, 8:41:38 AM
    Author     : gcastillo, nbenavidez
--%>
<%@page import="com.almori.hr.timetracker.*" import="java.text.*" import="java.util.Date" contentType="text/html" pageEncoding="UTF-8" %>
<%
    int action = Integer.parseInt(request.getParameter("action"));
    Employee me = new Employee();
    TimeTrackerBean b = new TimeTrackerBean();

    if (action == 1) {// action 1, login procedure
        try {
            String name = request.getParameter("login");
            String pass = request.getParameter("password");  //data coming from index.jsp
            //this page cannot be accessed from anywhere 
            if ((name == " " || pass == " ") || (name == null || pass == null)) {
                response.sendRedirect("index.jsp?error=1&null=1");
            } else {
                me = b.getEmployee(name, pass); // get the employee if it exist and setup a session

                session.setAttribute("Employee", me);
                if (me.getRole() == 1)//agent
                {
                    //<editor-fold defaultstate="collapsed" desc="comment">
                    response.sendRedirect("punchit.jsp");

                    //</editor-fold> response.sendRedirect("punchit.jsp");
                } else//admon
                {
                    response.sendRedirect("mgrdash.jsp");
                }
            }

        } catch (Exception e) { // any problems send user to log back in
            response.sendRedirect("index.jsp?error=1&excep=1");
        }
    } else if (action == 2) {// action 1, punch in/out procedure
        String reason = request.getParameter("reason");
        String status = request.getParameter("status");  //need to ID the person to punch in/out
        int id = Integer.parseInt(request.getParameter("id"));
        try {
            b.punchTimecard(id, status, reason); //punch
            me.setStatus(status);
            String mssg = "Exitosamente has " + me.getInverseStatus();
            response.sendRedirect("index.jsp?m=" + mssg); // redirect to login page with success mssg
        } catch (Exception e) {
            response.sendRedirect("index.jsp?error=2"); // cannot punch in or out
        }

    } else if (action == 3){ // SAVE TIMECARD CORRECTIONS
        String timeCardId = request.getParameter("_timeCardId");
        String inHrs = request.getParameter("_inHrs");
        String inMin = request.getParameter("_inMin");
        String inAMPM = request.getParameter("_inAMPM");
        String outHrs = request.getParameter("_outHrs");
        String outMin = request.getParameter("_outMin");
        String outAMPM = request.getParameter("_outAMPM");
        String strDate = request.getParameter("_strDate");
        String strInTime = inHrs +":"+ inMin +" "+ inAMPM;
        String strOutTime = outHrs +":"+ outMin +" "+ outAMPM;
        String strInDateTime = strDate +" "+ strInTime;
        String strOutDateTime = strDate +" "+ strOutTime;
        Date inTime = new SimpleDateFormat("MMM-dd-yyyy hh:mm a").parse(strInDateTime);
        Date outTime = new SimpleDateFormat("MMM-dd-yyyy hh:mm a").parse(strOutDateTime);
        String reason = request.getParameter("_reason");
        Timecard timeCard = new Timecard();
        timeCard.setId(Integer.parseInt(timeCardId));
        timeCard.setIn(inTime);
        timeCard.setOut(outTime);
        timeCard.setReason(reason);
        b.fixTimeCard(timeCard);
    } else {
        response.sendRedirect("index.jsp?error=1&else=1");
    }
%>
